-
Notifications
You must be signed in to change notification settings - Fork 79
Convert Tptr Dialect to LLVM #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| Path(ttshared_path).write_text(ttsharedir) | ||
| mlir_opt_path = _get_llvm_bin_path("mlir-opt") | ||
| # TritonShared-MLIR to LLVM-MLIR | ||
| subprocess.check_call([mlir_opt_path, ttshared_path, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we preserve all the original comments since they explain why we need certain passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not resolved, there are other comments here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't understand your meaning well before, I will add it back
|
I like the idea of this change but for it to be viable we need to limit the scope of its impact. Anything associated with TPTR is temporary until all the necessary features are in upstream ptr dialect. Please revert the edits that removed conversions from TT/TTS IR to ptr/tptr IR. An ideal situation would be just adding the TptrToLLVM pass that operates purely on upstream dialects plus tptr IR (no TT or TTS IR). Then have the pass manager you wrote instead of the opt invocation. @nhat-nguyen Do you think we'd want to keep around the old opt invocation for when we can use all upstream ptr dialect features or would it be fine to fully move to the pass manager approach here? |
|
I understand you want to keep the modifications within MLIR. The tptr dialect seems to be waiting for the full completion of MLIR's PtrDialect (I tried to modify tptr to use MLIR's ptr dialect, but mlir-opt does not yet recognize the ptr dialect, and the ptr dialect currently does not provide the ability to convert with other dialects). This method was used considering various reasons (given my extremely limited capabilities and perspective on MLIR and LLVM), and I welcome everyone's feedback |
|
The MLIR opt tool should recognize ptr dialect but is not expected to recognize tptr. The pass manager approach should recognize ptr dialect and if it does not then we need to register the ptr dialect somewhere. We do not need to necessarily use ptr dialect operations just yet, but we want to keep using ptr.ptr type. |
|
Yes, using pass manager approach, we could register the ptr dialect to its' context. |
|
Looking at the general Triton+TritonShared+CPU pipeline. There are three stages:
We should not add LLVM dialect to stage 1 or 2. This is mainly because it is a very low level dialect and might not mesh as well during the early stages of the stage 3 MLIR backends. Your TptrToLLVM pass would be running somewhere near the end of stage 3 so is fine to generate LLVM dialect. |
|
I am still getting familiar with mlir and llvm. |
|
If we can add this TPtrToLLVM conversion then the pass manager is worth it. This should hopefully enable dynamic pointer end-to-end compilation for the CPU backend. If we cannot support TPtrToLLVM conversion, then the pass manager does not have much benefit. |
|
Understandable |
|
I think the current position is good. If it helps to move it 1-3 earlier or later then that is also fine but try to keep it around the other *_to_llvm passes. Any tptr operation should be operation on integers, ptr.ptr types, and/or memref types. The changes to TritonToLinalgExperimental passes should be reverted so that Triton and TritonStructured operations and types do not reach stage 3. |
|
Understood, I will implement it |
|
Hi @red1bluelost I have new changes, Please review again |
d9fb190 to
9340b85
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New set of review comments.
| // Option<"typeoffsetToConst", "typeoffset-to-const", "bool", /*default*/"true", | ||
| // "Convert tptr.typeoffset to llvm.mlir.const">, | ||
| ]; | ||
| let dependentDialects = ["mlir::tptr::TPtrDialect", "mlir::LLVM::LLVMDialect"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect we will want ptr::PtrDialect included here eventually if not now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I previously tried using ptr.ptrtype, and I think when we use ptrdialect, we need to wait until Mr fabianmcg merges most of the ptr-related changes into LLVM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add ptr::PtrDialect to the list of 'dependentDialects' here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously I thought we only used ptrType so we didn't add PtrDialect, but of course we could
a627337 to
44759d9
Compare
|
It looks like there's an issue with the GitHub tool or with my push. I've resolved all the conversations, please take another look |
853c575 to
6a9fd30
Compare
|
Please do not resolve my comments. I will resolve them as I go through a re-review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase your changes onto an updated Triton Shared.
CMakeLists.txt
Outdated
|
|
||
| Python3::Module | ||
| pybind11::headers | ||
| ${Python3_LIBRARIES} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove Python3_LIBRARIES
| Path(ttshared_path).write_text(ttsharedir) | ||
| mlir_opt_path = _get_llvm_bin_path("mlir-opt") | ||
| # TritonShared-MLIR to LLVM-MLIR | ||
| subprocess.check_call([mlir_opt_path, ttshared_path, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not resolved, there are other comments here.
| // Option<"typeoffsetToConst", "typeoffset-to-const", "bool", /*default*/"true", | ||
| // "Convert tptr.typeoffset to llvm.mlir.const">, | ||
| ]; | ||
| let dependentDialects = ["mlir::tptr::TPtrDialect", "mlir::LLVM::LLVMDialect"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add ptr::PtrDialect to the list of 'dependentDialects' here.
lib/Dialect/TPtr/IR/TPtrOps.cpp
Outdated
| @@ -1,17 +1,18 @@ | |||
| #include "mlir/Interfaces/SideEffectInterfaces.h" // Required for IR/TPtrOps.h.inc | |||
| #include "mlir/Bytecode/BytecodeOpInterface.h" | |||
| #include "mlir/Interfaces/SideEffectInterfaces.h" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove all edits from this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have replaced it back to the original file style. Just to confirm, do I need to rebase back to the original commit and delete all modifications to the file in the commit to prevent the existence of commit records?
| } | ||
|
|
||
| #endif // TPTR_DIALECT | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove edits from this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as the previous conversation
|
|
||
| #define GEN_PASS_DECL | ||
| #include "triton-shared/Conversion/TPtrToLLVM/Passes.h.inc" | ||
| void populateTPtrToLLVMConversionPatterns(RewritePatternSet &patterns, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Put a new line between the include and function declaration.
| MLIRIR | ||
| MLIRPass | ||
| MLIRTransforms | ||
| MLIRSupport | ||
| MLIRReconcileUnrealizedCasts | ||
| TPtrIR | ||
| MLIRDialectUtils |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) alphabetical order
| mlir::triton::TritonDialect, | ||
| mlir::cf::ControlFlowDialect, mlir::scf::SCFDialect, | ||
| mlir::math::MathDialect, mlir::arith::ArithDialect, | ||
| // mlir::gpu::GPUDialect, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please run clang-format
|
Sorry, I will start making changes based on your suggestion now
|
6a9fd30 to
a2683b1
Compare
|
Hi @Realtyxxx and reviewer @red1bluelost just wanted to add some things I found about this PR that may prove useful. First of all thanks for the PR as It has come in handy for a downstream implementation I'm doing. However, I found a big error that makes some programs crash. @Realtyxxx at First of all it doesn't seem right that if you are lowering But the real big mistake is on // For now, use alloca instead of malloc to avoid complex call setup
Value totalSize = rewriter.create<LLVM::ConstantOp>(
loc, i64Ty, rewriter.getIntegerAttr(i64Ty, totalElements));
Value allocatedPtr = rewriter.create<LLVM::AllocaOp>(
loc, ptrTy, ptrTy, totalSize, /*alignment=*/0);What is being done here is incorrect as you are rewriting a What I suggest is to try and add some // Add address space conversions.
converter.addTypeAttributeConversion(
[&](PtrLikeTypeInterface type, ptr::GenericSpaceAttr memorySpace)
-> TypeConverter::AttributeConversionResult {
if (type.getMemorySpace() != memorySpace)
return TypeConverter::AttributeConversionResult::na();
return IntegerAttr::get(IntegerType::get(type.getContext(), 32), 0);
});
// Add type conversions.
converter.addConversion([&](ptr::PtrType type) -> Type {
std::optional<Attribute> maybeAttr =
converter.convertTypeAttribute(type, type.getMemorySpace());
auto memSpace =
maybeAttr ? dyn_cast_or_null<IntegerAttr>(*maybeAttr) : IntegerAttr();
if (!memSpace)
return {};
return LLVM::LLVMPointerType::get(type.getContext(),
memSpace.getValue().getSExtValue());
});Or just try and use malloc but I think just doing the I haven't tried to do it as I also work with a downstream LLVM that already has this. Anyways, I hope this comment can help! |
Thank you very much for your opinion, I have started to work on the research |
|
|
I understand what you mean, I'll give it a try, the reason I thought about this before was because I ran into the form |
a7c6c5c to
592454d
Compare
|
The branch has been rebased and squashed. Please take another look. @red1bluelost @Dasor |
592454d to
3df3d07
Compare
|
The malloc looks good, however, there is no conversion for you can use this example to test and see if func.func @kernel(%ptr: !ptr.ptr<#ptr.generic_space>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c128 = arith.constant 128 : index
%alloc_4 = memref.alloc() {alignment = 64 : i64} : memref<128x!ptr.ptr<#ptr.generic_space>>
scf.for %arg9 = %c0 to %c128 step %c1 {
memref.store %ptr, %alloc_4[%arg9] : memref<128x!ptr.ptr<#ptr.generic_space>>
}
memref.dealloc %alloc_4 : memref<128x!ptr.ptr<#ptr.generic_space>>
return
}
} |
using LLVMTypeConverter didn't change anything, ptr-to-llvm is handled along with other transformations in the convert-to-llvm pass, where it doesn't handle memref situations all at once |
|
Well I think that's the point if LLVM already handles that with I think once #331 gets merged and #329, #327 gets done the only part of TPtr we are going to need is |
I have a similar opinion. In the future, the tptr dialect in triton-shared may be replaced by the improved ptr dialect. It seems that my changes will be irrelevant. |

Directly, I want to solve the problem of tptr dialect drop, mlir-opt can't recognize tptr dialect, so I used pass manager;
Furthermore;
I refer to triton-cpu and triton-linalg to handle the conversion of unstructured ptr types to llvm;
I used nhat's code case to test, verified its correctness, and placed it in the test directory